package cn.edu.xhu.library_management.book.service.impl;

import cn.edu.xhu.library_management.book.client.AreaClient;
import cn.edu.xhu.library_management.book.entity.LmBookCategory;
import cn.edu.xhu.library_management.book.entity.dto.BookRackLocationDTO;
import cn.edu.xhu.library_management.book.entity.search.LmBookSearch;
import cn.edu.xhu.library_management.book.entity.vo.BookDetail;
import cn.edu.xhu.library_management.book.mapper.LmBookCategoryMapper;
import cn.edu.xhu.library_management.common.entity.R;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import cn.edu.xhu.library_management.book.entity.LmBook;
import cn.edu.xhu.library_management.book.service.LmBookService;
import cn.edu.xhu.library_management.book.mapper.LmBookMapper;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * @author 21086
 * @description 针对表【lm_book(图书表)】的数据库操作Service实现
 * @createDate 2023-03-15 10:17:50
 */
@Service
public class LmBookServiceImpl extends ServiceImpl<LmBookMapper, LmBook>
        implements LmBookService {
    /* minio 服务器地址 */
    @Value("${minio.server}")
    private String imgServer;
    @Value("${minio.port}")
    private int serverPort;
    @Resource
    private AreaClient areaClient;

    @Resource
    private LmBookCategoryMapper bookCategoryMapper;


    @Override
    public BookDetail findBookDetail(Long bookId) {
        BookDetail detail = new BookDetail();
        // 通过图书Id 查询图书
        LmBook book = this.getById(bookId);

        // 通过书架Id 获得详细位置信息
        BookRackLocationDTO location = areaClient.findBookRackLocationById(book.getBrId());
        // 获取图书分类
        LmBookCategory category = bookCategoryMapper.selectById(book.getBcId());
//        System.out.println((BookRackLocationDTO) r.getData().get("location"));
//        BookRackLocationDTO location = (BookRackLocationDTO) r.getData().get("location");
        BeanUtils.copyProperties(location, detail);
        BeanUtils.copyProperties(book, detail);
        BeanUtils.copyProperties(category, detail);
        // 查询当前图书分类
        return detail;
    }

    @Override
    public Page<LmBook> search(LmBookSearch search) {
        Page<LmBook> page = new Page<>(search.getCurrentPage(), search.getPageSize());
        QueryWrapper<LmBook> wrapper = new QueryWrapper<>();
        if (search.getBcId() != null) {
            // 用户有分类
            wrapper.lambda().eq(LmBook::getBcId, search.getBcId());
        }
        if (search.getBoName() != null) {
            // 书名模糊查询
            wrapper.lambda().like(LmBook::getBoName, search.getBoName());
        }
        if (search.getBoAuthor() != null) {
            // 作者模糊查询
            wrapper.lambda().like(LmBook::getBoAuthor, search.getBoAuthor());
        }
        if (search.getBoIsbn() != null) {
            // ISBN 编号查询
            wrapper.lambda().eq(LmBook::getBoIsbn, search.getBoIsbn());
        }
        if (search.getBoPress() != null) {
            // 出版社模糊查询
            wrapper.lambda().like(LmBook::getBoPress, search.getBoPress());
        }
        if (search.getBrId() != null) {
            // 书架查询
            wrapper.lambda().eq(LmBook::getBrId, search.getBrId());
        }
        if (search.getBrRow() != null) {
            // 排数查询
            wrapper.lambda().eq(LmBook::getBrRow, search.getBrRow());
        }
        Page<LmBook> bookPage = this.page(page, wrapper);
        return bookPage;
    }

    @Override
    public List<LmBook> findBookByCategory(Integer pid) {
        List<LmBook> books = new ArrayList<>();
        // 所有父Id集合
//        List<Integer> pids = new ArrayList<>();
        // 自求下一级
        List<LmBookCategory> categories = bookCategoryMapper.selectList(new QueryWrapper<LmBookCategory>().lambda().eq(LmBookCategory::getBcParantId, pid));
        categories.forEach(lmBookCategory -> {
            List<LmBook> list = this.list(new QueryWrapper<LmBook>().lambda().eq(LmBook::getBcId, lmBookCategory.getBcId()));
            if (!list.isEmpty()){
                // 有数据
                books.addAll(list);
            }
        });


        books.forEach(book -> book.setBoImage(imgServer + ":" + serverPort + book.getBoImage()));
        return books;
    }
}




